﻿using UnityEngine;
using System.Collections;

public class Movement : MonoBehaviour {

    public float speed;

	public GameObject hasFound;
	public GameObject foundMessage;

    public float maxVelocity;

    public float footAmount;
    public float maxFootSound;
    public CircleCollider2D footCollider;
    public Transform footSprite;

	public int offset;
	private AnimationManager anim;

	private bool isHorizontalAxis;
	private float value;
	private bool hasAnimation;

	private Fire fireManager;

	FadeIn fade;
	
	void Start()
	{

		anim = GetComponent<AnimationManager>();
		fireManager = GetComponent<Fire>();
	}

	void Update () 
	{
		int currentLayer = 0;

		float positionY = offset-transform.position.y;
		int intPositionY = ((int)positionY);

		float result = Mathf.Abs(positionY-intPositionY);

		if(result > .5f)
			currentLayer = (((int)(offset-transform.position.y+transform.localScale.y/2))*2)+2;
		else
			currentLayer = (((int)(offset-transform.position.y+transform.localScale.y/2))*2)+1;

		GetComponent<SpriteRenderer>().sortingOrder = currentLayer+1;

		VerifyAnimation();
		changeAnim();

		if (Input.GetAxisRaw("Horizontal") != 0)
        {
			if(!hasAnimation)
			{
				hasAnimation = true;
				isHorizontalAxis = true;
				value = Input.GetAxisRaw("Horizontal");
			}

            if (Mathf.Abs(rigidbody2D.velocity.x) < maxVelocity)
                rigidbody2D.AddForce(new Vector2(speed * Time.deltaTime * Input.GetAxisRaw("Horizontal"), 0));

            if (footCollider.radius < maxFootSound)
            {
                footCollider.radius += footAmount * Time.deltaTime;
				footSprite.localScale = new Vector3(footCollider.radius/2, footCollider.radius/2, transform.localScale.z);
            }
        }

        if (Input.GetAxisRaw("Vertical") != 0)
        {
			if(!hasAnimation)
			{
				hasAnimation = true;
				isHorizontalAxis = false;
				value = Input.GetAxisRaw("Vertical");

			}

            if (Mathf.Abs(rigidbody2D.velocity.y) < maxVelocity)
                rigidbody2D.AddForce(new Vector2(0,speed * Time.deltaTime * Input.GetAxisRaw("Vertical")));

            if (footCollider.radius < maxFootSound)
            {
                footCollider.radius += footAmount * Time.deltaTime;
				footSprite.localScale = new Vector3(footCollider.radius/2,footCollider.radius/2, transform.localScale.z);

            }
        }

        if ((rigidbody2D.velocity.x == 0 && rigidbody2D.velocity.y == 0) || (Input.GetAxisRaw("Vertical") == 0 && Input.GetAxisRaw("Horizontal") == 0))
        {
			if(Input.GetAxisRaw("Vertical") == 0 && Input.GetAxisRaw("Horizontal") == 0)
			{
				hasAnimation = false;
			}

            footCollider.radius = .25f;
			footSprite.localScale = new Vector3(footCollider.radius/2, footCollider.radius/2, transform.localScale.z);
            rigidbody2D.velocity = new Vector2(0, 0);
			changeIdleAnim();
        }
	}

	void changeIdleAnim()
	{
		if(isHorizontalAxis)
		{
			if(value > 0)
			{
				if(fireManager.hasArrow)
					anim.StartAnim("Idle_Arrow_Right");
				else
					anim.StartAnim("Idle_NoArrow_Right");
			}
			else if(value < 0)
			{
				if(fireManager.hasArrow)
					anim.StartAnim("Idle_Arrow_Left");
				else
					anim.StartAnim("Idle_NoArrow_Left");
			}
		}
		else
		{
			if(value > 0)
			{
				if(fireManager.hasArrow)
					anim.StartAnim("Idle_Arrow_Back");
				else
					anim.StartAnim("Idle_NoArrow_Back");
			}
			else if(value < 0)
			{
				if(fireManager.hasArrow)
					anim.StartAnim("Idle_Arrow_Front");
				else
					anim.StartAnim("Idle_NoArrow_Front");
			}
		}
	}

	void RemoveArrow()
	{
		if(isHorizontalAxis)
		{
			if(value > 0)
			{
				anim.StartAnim("Idle_NoArrow_Right");
			}
			else if(value < 0)
			{
				anim.StartAnim("Idle_NoArrow_Left");
			}
		}
		else
		{
			if(value > 0)
			{
				anim.StartAnim("Idle_NoArrow_Back");
			}
			else if(value < 0)
			{
				anim.StartAnim("Idle_NoArrow_Front");
			}
		}
	}

	void changeAnim()
	{
		if(isHorizontalAxis)
		{
			string animName = "";
			if(value > 0)
			{
				if(fireManager.hasArrow)
					animName = "Walk_Arrow_Right";
				else
					animName = "Walk_NoArrow_Right";
			}
			else if(value < 0)
			{
				if(fireManager.hasArrow)
					animName = "Walk_Arrow_Left";
				else
					animName = "Walk_NoArrow_Left";
			}

			if(!anim.HasAnimation(animName))
			{
				anim.StartAnim(animName);
			}
		}
		else
		{
			string animName = "";

			if(value < 0)
			{
				if(fireManager.hasArrow)
					animName = "Walk_Arrow_Front";
				else
					animName = "Walk_NoArrow_Front";
			}
			else if(value > 0)
			{
				if(fireManager.hasArrow)
					animName = "Walk_Arrow_Back";
				else
					animName = "Walk_NoArrow_Back";
			}

			if(!anim.HasAnimation(animName))
			{
				anim.StartAnim(animName);
			}
		}
	}

	private void VerifyAnimation()
	{
		if(isHorizontalAxis)
		{
			if(Input.GetAxisRaw("Horizontal") == 0)
				hasAnimation = false;

			if(value < 0)
			{
				if(Input.GetAxisRaw("Horizontal") > 0)
				{
					hasAnimation = true;
					isHorizontalAxis = true;
					value = Input.GetAxisRaw("Horizontal");
				}
			}
			else
			{
				if(Input.GetAxisRaw("Horizontal") < 0)
				{
						hasAnimation = true;
						isHorizontalAxis = true;
						value = Input.GetAxisRaw("Horizontal");
				}
			}
		}
		else
		{
			if(Input.GetAxisRaw("Vertical") == 0)
				hasAnimation = false;

			if(value < 0)
			{
				if(Input.GetAxisRaw("Vertical") < 0)
				{
					hasAnimation = true;
					isHorizontalAxis = false;
					value = Input.GetAxisRaw("Vertical");
				}
			}
			else
			{
				if(Input.GetAxisRaw("Vertical") > 0)
				{

					hasAnimation = true;
					isHorizontalAxis = false;
					value = Input.GetAxisRaw("Vertical");
				}
			}
		}

	}

	void HasFound()
	{
		GetComponent<CollisionManager>().enabled = false;
		GetComponent<AnimationManager>().enabled = false;
		GetComponent<Fire>().enabled = false;
		this.enabled = false;

		foundMessage.SetActive(true);
		hasFound.SetActive(true);
		Time.timeScale = 0;
	}
}
